<#assign module="sys"/>

<@override name="header">
</@override>
<@override name="body">

	<div class="row">
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">
					填写用户信息
			    </div>
			    <div class="panel-body">
    				<form id="submit_form" action="${ctx}/admin/create" class="form-horizontal" method="post">
		               
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>用户名：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="username" name="username" type="text" value="${admin.username?if_exists}" required placeholder="用户名为2-20个字符"/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>密码：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="password" name="password" type="password" value="${admin.password?if_exists}" required placeholder="密码长度为8到30个字符，采用数字、字母、特殊字符三种组合"/>
		                    </div>
		                </div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label"><em>*</em>重复密码：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="passwordconfirm" name="passwordconfirm" type="password" value="${admin.passwordconfirm?if_exists}" required placeholder="需要和密码一致"/>
		                    </div>
		                </div>
							<div class="form-group">
								<label class="col-sm-3 control-label"><em>*</em>所属公司：</label>
								<div class="col-sm-3">
									<select class="form-control" id="companyid" name="companyid" required >
										<#list companys as company>
											<option value="${company.id}">${company.companyName}</option>
										</#list>
									</select>
								</div>
							</div>
		                
		                <div class="form-group">
		                    <label class="col-sm-3 control-label">手机号码：</label>
		                    <div class="col-sm-3">
		                    	<input class="form-control" id="phone" name="phone" type="text" value="${admin.phone?if_exists}" placeholder="用于短信登录收取验证码"/>
		                    </div>
		                    <div class="col-sm-3 checkbox">
							    <label>
							      <input type="checkbox" id="phoneLogin" name="phoneLogin"> 开启短信登录
							    </label>
						  	</div>
		                </div>
		                
		                
		                <div class="form-group">
                            <label class="col-sm-3 control-label">是否锁定：</label>
                            <div class="col-sm-7">
                                <label>
                                	<p class="form-control-static">
								  		<input type="checkbox" name="isLocked" <#if admin.isLocked??>${admin.isLocked?string('checked="checked"','')}</#if> />
									</p>
								</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label"><em>*</em>角色选择：</label>
                            <div class="col-sm-7">
                            	<#list roles as role>
									<label style="padding-right:12px;">
										<p class="form-control-static">
											<input type="checkbox" value="${role.id}" name="roleIds"/> ${role.name}
										</p>
									</label>
								</#list>
                            </div>
                        </div>
    					
		                <div class="form-group" style="margin-left:10px;">
			                <label class="col-sm-3 control-label"></label>
			                <input type="submit" class="btn btn-primary" value="保存">
			                <input type="button" class="btn btn-default" onclick="window.location.href = '${ctx}/admin/list';" value="返回">
			            </div>
					</form>
        		</div>
			</div>
		</div>
	</div>

</@override>
<@override name="footer">
<script src="${ctx}/assets/js/md5.js"></script>
<script src="${ctx}/assets/js/base64.js"></script>
<script type="text/javascript">
	<#if message??>
		$.scojs_message("${message}", $.scojs_message.TYPE_ERROR);
	</#if>
	$(document).ready(function(){
		$("#submit_form").submit(function(){
			var account=$.trim($("#username").val());
			if(account==''){
				$.scojs_message("用户名不能为空", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if(account.length<2 || account.length>20){
				$.scojs_message("用户名长度为2-20个字符", $.scojs_message.TYPE_ERROR);
				return false;
			}
			
			var password=$.trim($("#password").val());
			if(password=='' || password.length<8 || password.length>30){
				$.scojs_message("密码长度为8到30个字符。", $.scojs_message.TYPE_ERROR);
				return false;
			}
			
			var password_regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}');
			if(!password_regex.test(password)){
				$.scojs_message("密码格式不正确，采用数字、字母、特殊字符三种组合", $.scojs_message.TYPE_ERROR);
				return false;
			}
			
			var passwordconfirm=$.trim($("#passwordconfirm").val());
			if(password!=passwordconfirm){
				$.scojs_message("密码不一致", $.scojs_message.TYPE_ERROR);
				return false;
			}
			
			if($("#phoneLogin").is(':checked')){
				if($("#phone").val().trim()==""){
					$.scojs_message("开启短信登录后需要填写手机号码", $.scojs_message.TYPE_ERROR);
					return false;
				}
			}
			
			if($("input[name=roleIds]:checked").length==0){
				$.scojs_message("至少选择一个角色", $.scojs_message.TYPE_ERROR);
				return false;
			}
			console.log($("#companyid").val());
			if(!$("#companyid").val()){
				$.scojs_message("请选择所属公司", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if($("#password").val() != ''){
				$("#password").val(new Base64().encode($.md5($("#password").val())));
			}
            if($("#passwordconfirm").val() != ''){
                $("#passwordconfirm").val(new Base64().encode($.md5($("#passwordconfirm").val())));
            }
			return true;
		});
	});
</script>
</@override>
<@extends name="../layout.ftl"/>